package com.qd.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.domain.entity.system.SystemSetDTO;
import com.qd.common.sys.enums.OperatorType;
import com.qd.system.service.system.SystemSetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统设置
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/systemSet")
@Slf4j
public class SystemSetController {
    private final SystemSetService systemSetService;

    /**
     * 添加系统设置
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:systemSet:save')")
    @PostMapping(value = "/save")
    @OperatorInfo(title = "添加系统设置", operator = OperatorType.INSERT)
    public SystemSetDTO save(@Validated({DefaultGroup.class}) @RequestBody SystemSetDTO dto) {
        dto.setAddTime(LocalDateTime.now());
        return systemSetService.save(dto);
    }

    /**
     * 修改系统设置
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:systemSet:edit')")
    @PutMapping(value = "/update")
    @OperatorInfo(title = "修改系统设置", operator = OperatorType.UPDATE)
    public SystemSetDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody SystemSetDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        return systemSetService.update(dto);
    }

    /**
     * 删除系统设置
     *
     * @param delDTO 主键，多个用分号隔开
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:systemSet:delete')")
    @DeleteMapping(value = "/delete")
    @OperatorInfo(title = "删除系统设置", operator = OperatorType.DELETE)
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            systemSetService.removeById(ids.get(0));
        } else {
            systemSetService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取系统设置
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public SystemSetDTO getById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return systemSetService.getByIdDto(id);
    }

    /**
     * 系统设置列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<SystemSetDTO> page(SystemSetDTO dto) {
        return systemSetService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 系统设置导出
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:systemSet:export')")
    @PostMapping(value = "/export")
    @OperatorInfo(title = "系统设置导出", operator = OperatorType.EXPORT)
    public void export(@RequestBody(required = false) SystemSetDTO query) {
        systemSetService.export(query);
    }
}